<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>系统管理<small>权限管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                    <el-breadcrumb-item>权限管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">


                    <div class="filter-container">
                        <el-input placeholder="权限名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="findPage1()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>



                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="selection"></el-table-column>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="权限名称" align="center"></el-table-column>

                        <el-table-column prop="keyword" label="权限关键字" align="center"></el-table-column>
                        <el-table-column prop="description" label="描述" align="center"></el-table-column>

                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>

                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="15">
                                        <el-form-item label="权限名称" prop="code">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>

                                </el-row>

                                <el-row>
                                    <el-col :span="15">
                                        <el-form-item label="权限关键字">
                                            <el-input v-model="formData.keyword"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="15">
                                        <el-form-item label="描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>


                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="15">
                                        <el-form-item label="权限名称" prop="code">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>

                                </el-row>

                                <el-row>
                                    <el-col :span="15">
                                        <el-form-item label="权限关键字">
                                            <el-input v-model="formData.keyword"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="15">
                                        <el-form-item label="描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>


                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>




                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
                    currentPage:1,//当前页码
                    pageSize:10,//每页显示的记录数
                    total:0,//总记录数
                    queryString:null//查询条件
                },
                dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见

            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                //VUE对象初始化完成后调用分页查询方法，完成分页查询
                this.findPage();
            },
            methods: {
                //编辑
                handleEdit() {
                    //对输入的表单内容进行校验dataEditForm,
                    //返回一个布尔类型的值
                    this.$refs['dataEditForm'].validate((valid) => {
                        if(valid){
                            //表单校验通过,进行下一步提交数据
                            //发送异步请求.res:表示后台响应回来的数据
                            axios.post("/permission/edit.do",this.formData).then((res) => {
                                if(res.data.flag){
                                    //数据提交成功,弹出成功提示信息
                                    this.$message({
                                        message:res.data.message,
                                        type:'success'
                                    });
                                }else{
                                    //数据提交失败,弹出错误提示信息
                                    this.$message.error(res.data.message);
                                }
                            }).finally(() => {
                                //当数据进行增删改操作,重新调用findPage方法进行分页查询
                                this.findPage();
                                //隐藏编辑窗口
                                this.dialogFormVisible4Edit = false;
                        });

                     }else{
                            //表单校验不通过
                            this.$message.error("表单校验失败!");
                            return false;
                        }
                    })

                },
                //添加
                handleAdd () {
                    //校验表单
                    this.$refs['dataAddForm'].validate((valid) => {
                        if(valid){
                            //表单校验,校验通过,发送ajax请求,将表单填写数据返回后台
                            ///checkitem/add.do
                            console.log(this.formData);
                            axios.post("/permission/add.do",this.formData).then((res) => {
                                //关闭新建窗口
                                this.dialogFormVisible = false;
                                if(res.data.flag){//执行保存信息成功
                                    //新增成功后，重新调用分页查询方法，查询出最新的数据
                                    this.findPage();
                                    //弹出提交成功信息
                                    this.$message({
                                        message:res.data.message,
                                        type:'success'
                                    });

                                }else{//执行保存信息失败
                                    //弹出提交失败信息
                                    this.$message.error(res.data.message);

                                }
                            }).finally(() => {//根据表单中"查询"去分页查询
                                //当数据进行增删改操作,重新调用findPage方法进行分页查询
                                this.findPage();
                            });

                        }else{//校验信息不通过
                            //弹出校验失败信息提示
                            this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                            return false;
                        }

                    })
                },

                //分页查询
                findPage() {
                    //传入后台的分页参数,封装为json对象
                    var param = {
                        currentPage:this.pagination.currentPage,//当前的页码
                        pageSize:this.pagination.pageSize,//每页显示的条数
                        queryString:this.pagination.queryString//查询条件
                    };
                    //将页面的参数发送ajax请求到后台
                    axios.post("/permission/findPage.do",param).then((res) => {//后台返回的数据
                        //给Vue中data赋值,当前页要展示的分页列表数据
                        this.pagination.total = res.data.total;
                        this.dataList = res.data.rows;
                    })
                },

                //bug处理,当通过条件查询设置默认为第一页,
                //查询时将当前页设置为第一页,
                findPage1(){
                    this.pagination.currentPage = 1;
                    //当数据进行增删改操作,重新调用findPage方法进行分页查询
                    this.findPage();
                },



                // 重置表单
                resetForm() {
                    //重置填写过的表单内容
                    this.formData = {};
                },

                // 弹出添加窗口
                handleCreate() {
                    //让新建表单框显示
                    this.dialogFormVisible = true;
                    //调用重置表单方法
                    this.resetForm();

                },

                // 弹出编辑窗口
                handleUpdate(row) {
                    //点击编辑按钮,发送ajax请求获取检查项信息
                    axios.get("/permission/findById.do?id=" + row.id).then((res)=>{//后台返回的json数据
                        if(res.data.flag){//后台返回的flag为true,进入进入编辑窗口
                            //dialogFormVisible4Edit为true编辑表单可见
                            this.dialogFormVisible4Edit = true;
                            //为模型数据设置值,回显
                            this.formData = res.data.data;
                        }else{//后台返回的flag为false,获取数据失败
                            this.$message.error("获取数据失败,请刷新页面或信息有误")
                        }
                    })

                },


                //切换页码
                handleCurrentChange(currentPage) {
                    //为分页条组件绑定current-change事件
                    //当页码改变时触发，对应的处理函数为handleCurrentChange
                    //当前页码切换后的页码
                    this.pagination.currentPage = currentPage;
                    //当数据进行增删改操作,重新调用findPage方法进行分页查询
                    this.findPage();
                },

                showMessage(r){
                    if(r == 'Error: Request failed with status code 403'){
                        //权限不足
                        this.$message.error('无访问权限');
                        return;
                    }else{
                        this.$message.error('未知错误');
                        return;
                    }
                },

                // 删除
                //row参数:表示当前行的json数据
                //{"age":"0-100","attention":"无","code":"0001","id":28,"name":"身高","price":5.0,"remark":"身高","sex":"0","type":"1"},
                handleDelete(row) {
                    // alert(row.id)
                   // prompt(row.id)
                   //  confirm("haha")

                    this.$confirm("你确定要删除?","提示",{
                        type:"warning"
                    }).then(()=>{
                        // alert("点击了确定")
                        //用户点击了确定按钮,发送ajax请求
                        axios.get("/permission/delete.do?id=" + row.id).then((res)=>{//后台响应的json数据
                            if(!res.data.flag){//响应的false成立,
                                //删除失败.响应错误信息
                                this.$message.error(res.data.message)
                            }else{
                                //删除成功,响应正确信息
                                this.$message({
                                    message:res.data.message,
                                    type:'success'
                                });
                                //重新加载最新页面.
                                this.findPage();
                            }
                        }).catch((r)=>{//权限不足,请求删除不成功的时候会执行
                            this.showMessage(r);
                        })

                    }).catch(()=>{
                        // alert("点击了取消")
                        this.$message({
                            type:'info',
                            message:'操作已取消'
                        });
                    });
                }
            }
        })
    </script>

</html>